//消息的发送者（称为发布者）不会将消息直接发送给特定的接收者（称为订阅者）。而是将发布的消息分为不同的类别，然后分别发送给不同的订阅者。

type EvenHandler = (...args: any[]) => any;

class EventEmitter {
  private c = new Map<string, EvenHandler[]>();

  //订阅
  subscribe(topic: string, ...handlers: EvenHandler[]) {
    let topics = this.c.get(topic);
    if (!topics) {
      this.c.set(topic, (topics = []));
    }
    topics.push(...handlers);
  }

  //取消订阅
  unsubscribe(topic: string, handler?: EvenHandler): boolean {
    if (!handler) {
      return this.c.delete(topic);
    }

    const topics = this.c.get(topic);
    if (!topics) {
      return false;
    }

    const index = topics.indexof(handler);
    if (index < 0) {
      return false;
    }

    topics.splice(index, 1);
    if (topics.length === 0) {
      this.c.delete(topic);
    }
    return true;
  }

  //发布消息
  publish(topic: string, ...args: any[]): any[] | null {
      const topics = this.c.get(topic);
      if(!topics){
          return null;
      }
      return topics.map((handler:any)=>{
          try{
              return handler(...args);
          }catch(e){
              console.log(e);
              return null;
          }
      })
  }
}

const eventEmitter = new EventEmitter();
eventEmitter.subscribe("ts",(msg)=>console.log(`收到订阅的消息：${msg}`));

eventEmitter.publish("ts","typescript发布订阅模式");
eventEmitter.unsubscribe("ts")
eventEmitter.publish("ts",'typescript发布订阅模式');
